package com.pureut.quality.service.impl;

import com.alibaba.nacos.shaded.com.google.common.base.Joiner;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pureut.common.core.exception.GlobalException;
import com.pureut.common.core.utils.bean.BeanUtils;
import com.pureut.quality.domain.AuditNonconformity;
import com.pureut.quality.domain.SchedulePlan;
import com.pureut.quality.domain.dto.AuditNonconformityDto;
import com.pureut.quality.domain.dto.SchedulePlanDto;
import com.pureut.quality.domain.vo.AuditNonconformityVo;
import com.pureut.quality.domain.vo.SchedulePlanVo;
import com.pureut.quality.domain.vo.SubmitVo;
import com.pureut.quality.mapper.AuditNonconformityMapper;
import com.pureut.quality.mapper.SchedulePlanMapper;
import com.pureut.quality.service.AuditNonconformityService;
import com.pureut.quality.service.SchedulePlanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/***
 * 外审不符合项报告
 * Author:C
 * Date:2023/01/19 17:15
 * @DESC
 */
@Service
public class SchedulePlanServiceImpl extends ServiceImpl<SchedulePlanMapper, SchedulePlan> implements SchedulePlanService {

    /**
     * 日程计划 数据层
     */
    @Resource
    SchedulePlanMapper schedulePlanMapper;

    /**
     * 查询日程计划列表
     * @param schedulePlanVo
     * @return
     */
    @Override
    public List<SchedulePlanDto> listAuditNonconformity(SchedulePlanVo schedulePlanVo) {
        return schedulePlanMapper.listSchedulePlan(schedulePlanVo);
    }

    /**
     * 查看日程计划
     * @param id
     * @return
     */
    @Override
    public SchedulePlanDto getInfo(Long id) {
        SchedulePlanDto info = schedulePlanMapper.getInfo(id);
        String auditBasis = info.getAuditBasis();
        String[] split = auditBasis.split(",");
        info.setAuditBasisList(Arrays.asList(split));
        return info;
    }

    /**
     * 新增日程计划
     * @param schedulePlanVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertAuditNonconformity(SchedulePlanVo schedulePlanVo) {
        List<String> auditBasisList = schedulePlanVo.getAuditBasisList();
        SchedulePlan schedulePlan = new SchedulePlan();
        BeanUtils.copyProperties(schedulePlanVo, schedulePlan);
        schedulePlan.setAuditBasis(Joiner.on(", ").join(auditBasisList));
        return save(schedulePlan);
    }

    /**
     * 修改日程计划
     * @param schedulePlanVo
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateAuditNonconformity(SchedulePlanVo schedulePlanVo) {
        List<String> auditBasisList = schedulePlanVo.getAuditBasisList();
        SchedulePlan schedulePlan = new SchedulePlan();
        BeanUtils.copyProperties(schedulePlanVo, schedulePlan);
        schedulePlan.setAuditBasis(auditBasisList.toString());
        return updateById(schedulePlan);
    }

    /**
     * 删除日程计划
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeAuditNonconformity(String ids) {
        return removeById(ids);
    }
}
